package com.zte.shopping.service.impl;

import com.zte.shopping.constant.DictConstant;
import com.zte.shopping.constant.StatusConstant;
import com.zte.shopping.entity.Dept;
import com.zte.shopping.entity.Staff;
import com.zte.shopping.entity.User;
import com.zte.shopping.exception.LoginDisabledException;
import com.zte.shopping.exception.NoPromissionException;
import com.zte.shopping.exception.RequestParameterException;
import com.zte.shopping.exception.StaffExistException;
import com.zte.shopping.mapper.staff.StaffMapper;
import com.zte.shopping.service.StaffService;
import com.zte.shopping.util.Constants;
import com.zte.shopping.util.MD5Util;
import com.zte.shopping.util.ParameterUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @program: ssm
 * @description: 实体类 管理员表
 * @author: zxb
 * @create: 2020-06-23 16:38
 **/
@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    StaffMapper staffMapper ;

    private  static  final Logger logger= Logger.getLogger(StaffService.class);


    @Override
    public Boolean findStaffByLoginName(String loginName) {
        boolean flag = false;
        int i = this.staffMapper.findStaffByLoginName(loginName);
        if (i > 0) {
            flag = true;
        }
        logger.debug("StaffServiceImpl--->findStaffByLoginName param:loginName="+loginName+" i="+i+" flag="+flag);
        return flag;
    }

    /**
     * 管理员登录 根据管理员用户和密码
     *
     * @param loginName
     * @param password
     * @return
     */
    @Override
    public Staff staffLogin(String loginName, String password) {
        logger.debug("StaffServiceImpl--->ftaffLogin param:loginName="+loginName+" password="+password);
        return this.staffMapper.staffLogin(loginName,password);
    }

    /**
     * @param pageIndex
     * @return java.util.List<com.zte.shopping.entity.Staff>
     * @Author zuti
     * @Description //TODO 管理员列表
     * @Date 16:46 2020/6/28
     * @Param [pageIndex]
     */
    @Override
    public List<Staff> staffList(Integer pageIndex) {
        int pageSize = Constants.pageSize;
        int from = (pageIndex-1)*pageSize;
        logger.debug("enter StaffServiceImpl ------> userList param: pageIndex="+pageIndex+" pageSize="+pageSize+" from="+from);
        return staffMapper.staffList(from,pageSize);
    }

    /**
     * @return int
     * @Author zuti
     * @Description //TODO 查询管理员人数
     * @Date 16:52 2020/6/28
     * @Param []
     **/
    @Override
    public int staffListCount() {
        logger.debug("enter StaffServiceImpl ------> staffListCount");
        return staffMapper.staffListCount();
    }

    @Override
    public List<Staff> searchStaffByParmaList(Integer pageIndex, String userName, String loginName, String phone, String email, Integer deptId, Integer isValid) {
        int pageSize = Constants.pageSize;
        int from = (pageIndex-1)*pageSize;
        logger.debug("enter StaffServiceImpl ------>searchStaffByParmaList params: pageIndex="+pageIndex+" pageSize="+pageSize+" from="+from
                +" userName="+userName+" loginName="+loginName+" phone="+phone+" email="+email+" deptId="+deptId+" isValid="+isValid);
        return staffMapper.searchStaffByParmaList(from,pageSize,userName,loginName,phone,email,deptId,isValid);
    }

    @Override
    public int staffListCountByParamList(String userName, String loginName, String phone, String email, Integer deptId, Integer isValid) {
        logger.debug("enter StaffServiceImpl ------>userListCountByParamList params: userName="+userName+
                " loginName="+loginName+" phone="+phone+" email="+email+" deptId="+deptId+" isValid="+isValid);
        return staffMapper.staffListCountByParamList(userName,  loginName,  phone,  email, deptId, isValid);
    }


    @Override
    public void modifyStaffStatus(String staffId, String isValid) {


        Integer staffStatus = Integer.parseInt(isValid);

        if (staffStatus == StatusConstant.STAFF_IS_VALID_DISABLE)
        {
            staffStatus = StatusConstant.STAFF_IS_VALID_ENABLE;
        }else
        {
            staffStatus =  StatusConstant.STAFF_IS_VALID_DISABLE;
        }

        logger.debug("enter StaffServiceImpl ------>modifyStaffStatus 向下传递的参数：staffId="+staffId+" staffStatus="+staffStatus);
        staffMapper.updateStaffStatus(Integer.parseInt(staffId), staffStatus);

    }

    @Override
    public void addStaff(Staff staff, String deptId, HttpSession session) throws StaffExistException, LoginDisabledException, NoPromissionException {
// 获取当前登录的员工
        Staff currentStaff = (Staff)session.getAttribute("staff");

        if (currentStaff == null)
        {
            throw new LoginDisabledException("登录失败,请重新登录");
        }

        if (!DictConstant.STAFF_ROLE_PREMISSION_SYSTEM_MANAGER.equals(currentStaff.getRole()))
        {
            throw new NoPromissionException("权限不足,您不是系统管理员");
        }

        // 根据账号查询员工信息
        Staff selectStaff = staffMapper.selectByLoginName(staff.getLoginName());
        // 判断该管理员账号   是否已经存在
        if (selectStaff != null)
        {
            throw new StaffExistException("该管理员账号已经存在");
        }

        // 设置员工所在部门
        Dept dept = new Dept();
        dept.setDeptId(Integer.parseInt(deptId));

        staff.setDept(dept);

        // 对密码进行加密
        staff.setPassword(MD5Util.md5(staff.getPassword()));

        // 创建的员工  为  有效   状态
        staff.setIsValid(StatusConstant.STAFF_IS_VALID_ENABLE);

        // 创建时间为当前时间
        staff.setCreateDate(new Date());

        // 创建人为当前登录员工
        staff.setCreateStaff(currentStaff);

        logger.debug("StaffImple============>addStaff 向下传递的参数为 staffName="+staff.getStaffName()+" deptId="+staff.getDept().getDeptId());

        // 添加管理员
        staffMapper.insertStaff(staff);
    }

    @Override
    public void modifyStaff(Staff staff, String deptId) throws RequestParameterException {
        if (ParameterUtil.isnull(staff.getStaffName()))
        {
            throw new RequestParameterException("管理员姓名不能为空!");
        }

        Dept dept = new Dept();
        dept.setDeptId(Integer.parseInt(deptId));

        staff.setDept(dept);
        logger.debug("StaffImple============>addStaff 向下传递的参数为 staffName="+staff.getStaffName()+" deptId="+staff.getDept().getDeptId());

        staffMapper.updateStaff(staff);
    }

    @Override
    public Staff findById(String staffId)  {

        logger.debug("StaffImple============>findById 向下传递的参数为 staffId="+staffId);

        Staff staff = staffMapper.selectById666(Integer.valueOf(staffId));
        logger.debug("StaffImple============>findById 返回的的结果为 staffId="+staff.getStaffId()
                +" staffName="+staff.getStaffName());
        return staff;
    }


}
